import wx
import configparser
import subprocess
import threading
"""pyinstaller -F --win-no-prefer-redirects client.py"""

"""https://blog.csdn.net/u013600225/article/details/52088196"""
"""netsh interface portproxy add v4tov4 listenaddress=192.168.6.31 listenport=19000 connectaddress=192.168.6.83 connectport=3389"""


class Main(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"客户端设置",pos = (100,200),size = (600,400))
        self.p = wx.Panel(self, wx.ID_ANY)
        
        
        wx.StaticText(self.p,label="服务器地址",pos=(10,10))
        self.add_text = wx.TextCtrl(self.p,value="",pos = (80,10),size = (100,30))
        wx.StaticText(self.p,label="服务器端口",pos=(190,10))
        self.remote_port_text = wx.TextCtrl(self.p,value="",pos = (255,10),size = (50,30))
        wx.StaticText(self.p,label="链接端口",pos=(310,10))
        self.port_text = wx.TextCtrl(self.p,value="",pos = (365,10),size = (50,30))
        wx.StaticText(self.p,label="密钥",pos=(420,10))
        self.token_text = wx.TextCtrl(self.p,value="",pos = (460,10),size = (50,30))
        save_button = wx.Button(self.p,label = "链接",pos = (520,10),size = (50,24))
        save_button.Bind(wx.EVT_BUTTON,self.savefile)
        

        self.content_text= wx.TextCtrl(self.p,pos = (5,45),size = (475,300),style = wx.TE_MULTILINE)
        self.content_text.SetValue("按win+R，输入mstsc，输入服务器地址+端口\n输入你电脑账号密码即可。\n查看后面提示 success则连接成功")

    
    def savefile(self,event):
    
        serve_add = self.add_text.GetValue()
        serve_prot = self.port_text.GetValue()
        serve_token = self.token_text.GetValue()
        remote_port_text = self.remote_port_text.GetValue()
        
        
        
        if serve_prot =='':
            self.content_text.SetValue("端口设置错误\n")
            return
        if serve_token =='':
            self.content_text.SetValue("密钥设置错误\n")
            return
        if serve_add =='':
            self.content_text.SetValue("地址设置错误\n")
            return
        
        with open('c.ini', "r+") as f:
            f.truncate()
        
        cfg1 = "c.ini"

        conf = configparser.ConfigParser()
        conf.read(cfg1)

        conf.add_section("common")
        conf.sections()
        conf.set("common","privilege_token",serve_token)
        conf.set("common","server_addr",serve_add)
        conf.set("common","server_port",remote_port_text)
        RDP_NAME = "RDP_"+serve_prot
        conf.add_section(RDP_NAME)
        conf.sections()
        conf.set(RDP_NAME,"type",'tcp')
        conf.set(RDP_NAME,"local_ip",'0.0.0.0')
        conf.set(RDP_NAME,"local_port",'3389')
        conf.set(RDP_NAME,"remote_port",serve_prot)
        conf.write(open("c.ini","w+"))
        self.content_text.SetValue("配置已修改\n")
        
        RunService(self).start()
        
    def setMessageText(self,msg):
        self.content_text.SetValue(msg)
        
        


    
class RunService(threading.Thread):
    def __init__(self,windows):
        threading.Thread.__init__(self)
        threading.Event().clear()
        self.windows = windows
        
    def run(self):
        wx.CallAfter(self.windows.setMessageText,"服务已启动\n")
        retcode = subprocess.call(["c.exe", "-c","c.ini"])
        
           

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Main()
    frame.Show()
    app.MainLoop()

